import 'dart:io';

import 'package:filetest/appglobal.dart';
import 'package:filetest/fileutils.dart';
import 'package:filetest/timeutils.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    createAppFolder();
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FlatButton(
                onPressed: (){

                  FileUtils.shared.writeBuiness("我是操作日志");

                },
                child: const Text("写入操作日志")
            ),
            FlatButton(
                onPressed: (){
                  FileUtils.shared.writeException("我是异常日志");
                },
                child: const Text("写入异常日志")
            ),
            FlatButton(
                onPressed: (){
                  FileUtils.shared.writeNetWork("我是网络日志");
                },
                child: const Text("写入网络日志")
            ),
            FlatButton(
                onPressed: ()async{
                  String content = await FileUtils.shared.readFileAllContent(AppGlobal.currentDayLogPath+"/buiness.txt");
                  debugPrint("content:"+content);
                },
                child: const Text("读取文件所有内容"),
            ),
            FlatButton(
              onPressed: ()async{
                String value = await FileUtils.shared.readKeyValue(AppGlobal.configIniPath,"customConfig5");
                debugPrint("value:"+value);
              },
              child: const Text("根据键读取文件内容"),
            ),
            FlatButton(
              onPressed: ()async{
                bool value = await FileUtils.shared.writeKeyValue(AppGlobal.configIniPath,"customConfig5","N");
                debugPrint("value:"+value.toString());
              },
              child: const Text("根据键写入文件内容"),
            ),
          ],
        ),
      ),

    );
  }
  //创建文件夹
  createAppFolder()async{
    String date = TimeUtils.getDate(format: "YYYYMMDD");
    if(Platform.isAndroid){
      //Android
      AppGlobal.packagePath = await FileUtils.shared.androidPackagePath;

    }
    if(Platform.isIOS){
     //IOS
      AppGlobal.packagePath = await FileUtils.shared.localDocPath;

    }
    AppGlobal.appPath = AppGlobal.packagePath+"/MobileManager";
    AppGlobal.configPath = AppGlobal.packagePath+"/MobileManager/ConfigFile";
    AppGlobal.configIniPath = AppGlobal.packagePath+"/MobileManager/ConfigFile/Config.ini";
    AppGlobal.logPath = AppGlobal.packagePath+"/MobileManager/Logs";
    AppGlobal.currentDayLogPath = AppGlobal.packagePath+"/MobileManager/Logs/"+ date;
    AppGlobal.tempPath = AppGlobal.packagePath+"/MobileManager/Temp";
    if(!await FileUtils.shared.judgeFileExist(AppGlobal.appPath) ){
      await FileUtils.shared.setFileFolder(AppGlobal.appPath);
    }
    if(!await FileUtils.shared.judgeFileExist(AppGlobal.configPath) ){
      await FileUtils.shared.setFileFolder(AppGlobal.configPath);
    }
    if(!await FileUtils.shared.judgeFileExist(AppGlobal.logPath) ){
      await FileUtils.shared.setFileFolder(AppGlobal.logPath);
    }
    if(!await FileUtils.shared.judgeFileExist(AppGlobal.currentDayLogPath) ){
      await FileUtils.shared.setFileFolder(AppGlobal.currentDayLogPath);
    }
    if(!await FileUtils.shared.judgeFileExist(AppGlobal.tempPath) ){
      await FileUtils.shared.setFileFolder(AppGlobal.tempPath);
    }
  }
}
